package net.jsoft.pms.job.problem;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import net.jsoft.platform.core.util.spring.SpringUtil;
import net.jsoft.platform.schedule.job.BaseJob;
import net.jsoft.pms.service.mysql.problem.IProblemService;

@Component
public class GetProblemJob extends BaseJob
{

    //    @Resource(name = "problemServiceImpl")
    //    IProblemService service;

    @Override
    public void doExecute(JobExecutionContext context) throws JobExecutionException
    {
        HttpClient httpClient = HttpClients.createDefault();

        try
        {
            //登录
            String loginData = "{\"tid\":\"\",\"ctrl\":\"XTGL001LoginCtrl_handleLogin?sName=XTGL001LoginCtrl_handleLogin#*^@^*#rUUID=FVjjr8YoLm3lse0dhKFwPlgqM4cKWw9H\",\"page\":null,\"data\":[{\"sword\":\"attr\",\"name\":\"userName\",\"value\":\"zhaojiping\"},{\"sword\":\"attr\",\"name\":\"userPwd\",\"value\":\"Abcd1234\"}],\"bindParam\":true}";
            String url = "http://10.23.8.160:8001/ajax.sword?sName=XTGL001LoginCtrl_handleLogin";
            HttpPost httpPost = new HttpPost(url);
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("postData", loginData));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8");
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);

            IProblemService service = SpringUtil.getBean("problemServiceImpl", IProblemService.class);

            //导出Excel文档
            String queryData = "%7B%22tid%22%3A%22%22%2C%22ctrl%22%3A%22SJ003sjcxCtrl_exportAllSjxxExcel%3FsName%3DSJ003sjcxCtrl_exportAllSjxxExcel%26rUUID%3DuqPQsCGjImxeziKAPNCWIu829azWqaK2%26gndm%3DF000BZPT00000130%26gwssswjg%3D00000000001%22%2C%22page%22%3A%22%22%2C%22data%22%3A%5B%7B%22name%22%3A%22mark%22%2C%22value%22%3A%22%22%2C%22sword%22%3A%22attr%22%7D%2C%7B%22sword%22%3A%22SwordForm%22%2C%22name%22%3A%22cxtjForm%22%2C%22data%22%3A%7B%22sjbm%22%3A%7B%22value%22%3A%22%22%7D%2C%22sjztDm%22%3A%7B%22value%22%3A%2202%2C03%22%7D%2C%22sjdlDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22sjxlDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22sjsfDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22dsDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22dcztDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22yxcdDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22sjlyDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22cljgDm%22%3A%7B%22value%22%3A%2200000000001%22%7D%2C%22ssxmDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22clgwDm%22%3A%7B%22value%22%3A%224b01aa9ed2e5492db41380b150a97467%2C46069833e1b64fa7818a95384317eaea%2C8ea0b19ceb4043d1a1f6c973f7ea3743%22%7D%2C%22ssywyDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22clrDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22tjrjgDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22tjgwDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22tjrDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22tjsj%22%3A%7B%22value%22%3A%22%22%7D%2C%22tjsjz%22%3A%7B%22value%22%3A%22%22%7D%2C%22gbDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22clfsDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22sjbt%22%3A%7B%22value%22%3A%22%22%7D%2C%22xxms%22%3A%7B%22value%22%3A%22%22%7D%2C%22drsjbm%22%3A%7B%22value%22%3A%22%22%7D%2C%22wxtsjid%22%3A%7B%22value%22%3A%22%22%7D%2C%22zxlxDm%22%3A%7B%22value%22%3A%22%22%7D%2C%22bbh%22%3A%7B%22value%22%3A%22%22%7D%7D%7D%5D%2C%22bindParam%22%3Atrue%7D";
            String downloadUrl = "http://10.23.8.160:8001/download.sword?sName=SJ003sjcxCtrl_exportAllSjxxExcel";
            HttpGet httpGet = new HttpGet(downloadUrl + "&postData=" + queryData);
            response = httpClient.execute(httpGet);
            HttpEntity he = response.getEntity();
            if (he != null)
            {
                InputStream in = he.getContent();
                service.batchImport(in);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
